<template>
    
    <!-- 组件的模板 -->
    <div class="page attendance-set show-footerBtn">
        <div class="section bg-color">
            <div class="weui-cells">
                <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectStartTime()">
                    <div class="weui-cell__bd">
                        <p>开始时间</p>
                    </div>
                    <div class="weui-cell__ft">{{work_week.start_date}}</div>
                </a>
                <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectEndTime()">
                    <div class="weui-cell__bd">
                        <p>结束时间</p>
                    </div>
                    <div class="weui-cell__ft">{{work_week.end_date}}</div>
                </a>
                <ul class="week-list com-border-top" id="weekList" v-if="work_week.start_date && work_week.end_date">
                    <li><span :class="{'selected':ischecked1}" @click="selectWeek('1')">周一</span></li>
                    <li><span :class="{'selected':ischecked2}" @click="selectWeek('2')">周二</span></li>
                    <li><span :class="{'selected':ischecked3}" @click="selectWeek('3')">周三</span></li>
                    <li><span :class="{'selected':ischecked4}" @click="selectWeek('4')">周四</span></li>
                    <li><span :class="{'selected':ischecked5}" @click="selectWeek('5')">周五</span></li>
                    <li><span :class="{'selected':ischecked6}" @click="selectWeek('6')">周六</span></li>
                    <li><span :class="{'selected':ischecked7}" @click="selectWeek('7')">周日</span></li>
                </ul>
            </div>
        </div>
        <div class="btn-box bg-color fixed" @click="submit"><a href="javascript:;" class="weui-btn weui-btn_primary">保存</a></div>
    </div>
        
</template> 

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import widget from 'gapi/widget'
    import clientUi from 'gapi/clientUi.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                /*按周设置*/
                work_week:{
                    start_date:'',
                    end_date:'',
                    week:[]
                },
                ischecked1:false,
                ischecked2:false,
                ischecked3:false,
                ischecked4:false,
                ischecked5:false,
                ischecked6:false,
                ischecked7:false
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                
                cache.getMemoryCache('workWeek', (data) => {
                    // console.log(data)
                    if(data.week){
                        this.work_week = data
                        let t = this.unique(data.week)
                        t.forEach( (item) => {
                            this.selectWeek(item) 
                        })
                    }
                }) 
            },
            unique (arr){
                let tmp = new Array();
                for(var i in arr){
                    //该元素在tmp内部不存在才允许追加
                    if(tmp.indexOf(arr[i])==-1){
                        tmp.push(arr[i]);
                    }
                }
                return tmp;
            },
            submit(){
                let sWeek = []
                if(this.ischecked1 == true){
                    sWeek.push('1')
                }
                if(this.ischecked2 == true){
                    sWeek.push('2')
                }
                if(this.ischecked3 == true){
                    sWeek.push('3')
                }
                if(this.ischecked4 == true){
                    sWeek.push('4')
                }
                if(this.ischecked5 == true){
                    sWeek.push('5')
                }
                if(this.ischecked6 == true){
                    sWeek.push('6')
                }
                if(this.ischecked7 == true){
                    sWeek.push('7')
                }
                this.work_week.week = sWeek
                if(this.work_week.start_date&&this.work_week.end_date&&this.work_week.week.length){
                    commonPage.goBack(1,{'key':'workWeek','value':this.work_week,'pream2':22})
                }else{
                     widget.toast('请设置日期和工作日')
                }
            },
        
/****************************************按周设置**********************************************/
            selectStartTime(){
                var self = this;
                let date = new Date()
                let currentDate = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()
                let maxDate = this.work_week.end_date?this.work_week.end_date:'3000-12-12'
                KCApiApp.showDatePicker(
                        currentDate,//  最小时间
                        maxDate,//  最大时间
                        self.work_week.start_date, //默认时间
                        (data)=>{    
                            self.work_week.start_date = data.SelectedDate
                        })
            },
            selectEndTime(){
                var self = this;
                let date = new Date()
                let currentDate = this.work_week.start_date
                let maxDate = (date.getFullYear()+1)+"-"+(date.getMonth()+1)+"-"+date.getDate()
                KCApiApp.showDatePicker(
                        currentDate,//  最小时间
                        maxDate,//  最大时间
                        this.work_week.end_date, //默认时间
                        (data)=>{                  //回调方法
                            this.work_week.end_date = data.SelectedDate
                        })
            },
            selectWeek (way){
                switch(way){ 
                    case '1':
                        if(this.ischecked1 == true){
                            this.ischecked1 = false
                            this.work_week.week.pop('1')
                        }else{
                            this.ischecked1 = true
                            this.work_week.week.push('1')
                        }
                        break
                    case '2':
                        if(this.ischecked2 == true){
                            this.ischecked2 = false
                            this.work_week.week.pop('2')
                        }else{
                            this.ischecked2 = true
                            this.work_week.week.push('2')
                        }
                        break
                    case '3':
                        if(this.ischecked3 == true){
                            this.ischecked3 = false
                            this.work_week.week.pop('3')
                        }else{
                            this.ischecked3 = true
                            this.work_week.week.push('3')
                        }
                        break
                    case '4':
                        if(this.ischecked4 == true){
                            this.ischecked4 = false
                            this.work_week.week.pop('4')
                        }else{
                            this.ischecked4 = true
                            this.work_week.week.push('4')
                        }
                        break
                    case '5':
                        if(this.ischecked5 == true){
                            this.ischecked5 = false
                            this.work_week.week.pop('5')
                        }else{
                            this.ischecked5 = true
                            this.work_week.week.push('5')
                        }
                        break
                    case '6':
                        if(this.ischecked6 == true){
                            this.ischecked6 = false
                            this.work_week.week.pop('6')
                        }else{
                            this.ischecked6 = true
                            this.work_week.week.push('6')
                        }
                        break
                    case '7':
                        if(this.ischecked7 == true){
                            this.ischecked7 = false
                            this.work_week.week.pop('7')
                        }else{
                            this.ischecked7 = true
                            this.work_week.week.push('7')
                        }
                        break
                    default:
                        break
                }
            }

        },
        created(){
            this.init();
            // window.loadData = this.updateDate
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/calendar.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/attendance-set.css"></style>






